<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src='../../../JS/detailView.js'></script>
		<style type="text/css">
			.do_button {
				width: 100px;
				border-radius: 5px;
				margin-left: 45%;
				margin-top: 30px;
			}
		</style>
	</head>

	<body style="padding: 0px;">

		<div class="easyui-layout" style="width:100%;height:500px;">
			<div data-options="region:'north'">
				<div id="toolbar" class="receipts_toolbar_style"></div>
				<form id="ff" method="post">
					<div id="p" class="easyui-panel" title="查询" style="width:100%;height:90px;padding:15px;">
						<table cellspacing="4">
							<tr>
								<td>用户组</td>
								<td><input class="easyui-combobox" id="search_customer_type" name="CUSTOMER_TYPE" placeholder="请输入需要查询的用户组" data-options="panelHeight:'100px',url:RemoteLinkAddress+'/logistics/customertype/listall.do',mode:'remote',method:'get',valueField:'ID',textField:'CUSTOMER_TYPE'" /></td>
								<td>配送开始时间</td>
								<td><input class="easyui-datebox" id="search_distribution_date_start" name="DISTRIBUTION_DATE" placeholder="请输入需要查询的配送时间"  /></td>
								<td>配送结束时间</td>
								<td><input class="easyui-datebox" id="search_distribution_date_end" name="DISTRIBUTION_DATE" placeholder="请输入需要查询的配送时间"  /></td>
								<td>
									<a href="#" class="easyui-linkbutton" onclick="doSearch()" data-options="iconCls:'icon-search'" style="width:80px;">查询</a>
								</td>
							</tr>
						</table>
					</div>
				</form>
			</div>
			<div data-options="region:'center'">
				<div>
					<table id="tt" class="table_style" style="width: 98%;height: 400px;">
					</table>
				</div>
			</div>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script>
		var currData; //当前数据
		var _detailToolbarData;
		_detailToolbarData = AddDetailToolBar(6);
		var data = [{
			"icon": "icon-redo",
			"func": "ProcessOrder()",
			"functionname": "生成采购单明细"
		}];
		_toolbar = AddReceiptsToolBar(0, data);
		$("#toolbar").append(_toolbar);

		$(document).ready(function() {
			$.messager.alert("消息", "因涉及库房选择，请按用户组查询后，按用户组分类生成采购单");
			loadRemote("");
		});

		function loadRemote(data) { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断		
			$.post(RemoteLinkAddress + "/logistics/salesorder/getPSalesT.do?ISVERIFY=1", data,
				function(result) {
					currData = handleData(result);
					$('#tt').datagrid('loadData', currData); //调用easyui的loadData方法，将数据写入id为tt的datagrid中
				},
				'json');
		}

		//生成采购单
		function ProcessOrder() {
			var currPurchaseDetailData = new Object();
			currPurchaseDetailData.ids = "";
			currPurchaseDetailData.rows = new Array();
			for(var i = 0; i < currData.length; i++) {
				for(var j = 0; j < currData[i].rows.length; j++) {
					var s = currData[i].rows[j];
					if(s.SELECTED) {
						currPurchaseDetailData.CUSTOMER_TYPE = s.CUSTOMER_TYPE;
						currPurchaseDetailData.DISTRIBUTION_DATE = s.DISTRIBUTION_DATE;
						currPurchaseDetailData.TYPE_ID = s.CUSTOMERTYPE_ID;
						//if(currPurchaseDetailData.ids.indexOf(s.INTERNALTRADE_ID) == -1) {
						if(StrInArray(currPurchaseDetailData.ids, s.INTERNALTRADE_ID) == -1) {
							currPurchaseDetailData.ids = currPurchaseDetailData.ids + s.INTERNALTRADE_ID + ";";
							var len = currPurchaseDetailData.rows.length;
							currPurchaseDetailData.rows[len] = new Object();
							var m = currPurchaseDetailData.rows[len]
							m.INTERNALTRADE_ID = s.INTERNALTRADE_ID;
							m.INTERNAL_NAME = s.INTERNAL_NAME;
							m.AMOUNT = s.AMOUNT;
							m.UNIT_NAME = s.UNIT_NAME;
							m.INTERNAL_TYPE = s.INTERNAL_TYPE;
							m.STOCKS = 0; //库存量
							m.EVALUATE_AMOUNT = s.AMOUNT;
							m.salesDetailIds = s.ID + ";";
						} else {
							
							for(var t = 0; t < currPurchaseDetailData.rows.length; t++) {
								var m = currPurchaseDetailData.rows[t];
								if(m.INTERNALTRADE_ID == s.INTERNALTRADE_ID) {
									m.AMOUNT = parseFloat(m.AMOUNT) + parseFloat(s.AMOUNT);
									m.salesDetailIds = m.salesDetailIds + s.ID + ";";
								}
							}
						}
					}
				}
			}
			if(currPurchaseDetailData.rows.length === 0) {
				$.messager.alert("警告", "所选的数据不能为空!");
				return;
			}
			res = JSON.stringify(currPurchaseDetailData);
			localStorage.setItem('PurchaseDetailData', res);
			AddTabIn("创建采购单明细", parent.$('#tabs'), "UI/LOGISTICS/PurchaseOrderCreate/PurchaseOrderConfirmCreate.html");
		}
		
		$('#tt').datagrid({
			view: detailview,
			width: '100%',
			height: 500,
			columns: [
				[{
					field: 'INTERNAL_TYPE',
					title: '种类',
					width: '50%',
					align: 'left'
				}, {
					field: 'ROWNUM',
					title: '未完成订单数',
					width: '50%',
					align: 'left',
					formatter: function(value, row, index) {
						if(value) {
							return value + "条";
						} else {
							return "0条";
						}
					}
				}]
			],
			detailFormatter: function(index, row) {
				return '<div style="padding:2px"><table class="ddv"></table></div>';
			},
			onExpandRow: function(index, row) {
				var currDataRow = currData[index].rows;
				var ddv = $(this).datagrid('getRowDetail', index).find('table.ddv');
				ddv.datagrid({
					data: currDataRow,
					fitColumns: true,
					singleSelect: false,
					rownumbers: true,
					loadMsg: '',
					height: 'auto',
					columns: [
						[{
							field: 'ck',
							checkbox: 'true',
						}, {
							field: 'ID',
							title: 'ID',
							width: '3%'
						}, {
							field: 'CUSTOMER_TYPE',
							title: '用户组',
							width: '10%',
							align: 'center'
						}, {
							field: 'CUSTOMER_NAME',
							title: '订货单位',
							width: '15%',
						}, {
							field: 'INTERNAL_NAME',
							title: '内部商品名称',
							width: '15%',
							align: 'center'
						}, {
							field: 'UNIT_NAME',
							title: '单位',
							width: '10%',
							align: 'center'
						}, {
							field: 'AMOUNT',
							title: '数量',
							width: '15%',
							align: 'center'
						}, {
							field: 'DISTRIBUTION_DATE',
							title: '配送时间',
							width: '15%',
							align: 'center'
						}, ]
					],
					onResize: function() {
						$('#dg').datagrid('fixDetailRowHeight', index);
					},
					onLoadSuccess: function() {
						setTimeout(function() {
							$('#dg').datagrid('fixDetailRowHeight', index);
						}, 0);
						/////设定某些条目被选中
						var s = row.rows;
						for(var i = 0; i < s.length; i++) {
							if(s[i].SELECTED) {
								$(this).datagrid('checkRow', i);
							}
						}
					},
					onCheck: function(index, row) {
						currDataRow[index].SELECTED = true;
					},
					onUncheck: function(index, row) {
						currDataRow[index].SELECTED = false;
					},
					onCheckAll: function(row) {
						for(var i = 0; i < row.length; i++) {
							currDataRow[i].SELECTED = true;
						}
					},
					onUncheckAll: function(row) {
						for(var i = 0; i < row.length; i++) {
							currDataRow[i].SELECTED = false;
						}
					}
				});
				$('#dg').datagrid('fixDetailRowHeight', index);
			}
		});

		function doSearch() {
			var value = $("#search_customer_type").combobox("getText").trim();
			var distributionStartDate = $("#search_distribution_date_start").datebox("getText").trim();
			var distributionEndDate = $("#search_distribution_date_end").datebox("getText").trim();
			var data = {};
			data["CUSTOMER_TYPE"] = value;
			data["DISTRIBUTION_DATE_START"] = distributionStartDate;
			data["DISTRIBUTION_DATE_END"] = distributionEndDate;
			loadRemote(data);
		}

		//接收数据并进行扁平化处理
		function handleData(resdata) {
			var data = new Array();
			for(var i = 0; i < resdata.length; i++) {
				data[i] = new Object();
				data[i].INTERNAL_TYPE = resdata[i].INTERNAL_TYPE;
				data[i].rows = new Array();
				for(var m = 0; m < resdata[i].orderRows.length; m++) {
					var resrow = resdata[i].orderRows[m];
					var _CUSTOMER_NAME = resrow.CUSTOMER_NAME;
					var _CUSTOMER_TYPE = resrow.CUSTOMER_TYPE;
					var __CUSTOMER_TYPE_ID = resrow.CUSTOMERTYPE_ID;
					data[i].DISTRIBUTION_DATE = resrow.DISTRIBUTION_DATE;
					var _index = data[i].rows.length;
					for(var j = 0; j < resrow.rows.length; j++) {
						var s = resrow.rows[j];
						if(s.STATE == "下单") { ///此处分清楚是明细的状态还是订单的状态
							data[i].rows[_index] = new Object();
							data[i].rows[_index].CUSTOMER_NAME = _CUSTOMER_NAME;
							data[i].rows[_index].CUSTOMER_TYPE = _CUSTOMER_TYPE;
							data[i].rows[_index].DISTRIBUTION_DATE = resrow.DISTRIBUTION_DATE;
							data[i].rows[_index].ID = s.ID;
							data[i].rows[_index].INTERNAL_TYPE = resdata[i].INTERNAL_TYPE;
							data[i].rows[_index].INTERNAL_NAME = s.INTERNAL_NAME;
							data[i].rows[_index].INTERNALTRADE_ID = s.INTERNALTRADE_ID;
							data[i].rows[_index].UNIT_NAME = s.UNIT_NAME;
							data[i].rows[_index].AMOUNT = s.AMOUNT;
							data[i].rows[_index].SELECTED = false;
							data[i].rows[_index].CUSTOMERTYPE_ID = __CUSTOMER_TYPE_ID;
							data[i].rows[_index].STATE = s.STATE; //下单状态
							_index = _index + 1;
						}
					}
					data[i].ROWNUM = _index;
				}
			}
			return data;
		}

		function StrInArray(ids, id) {
			var s = ids.split(";");
			for(var i=0;i<s.length;i++)
			{
				if(id==s[i])
				{
					return i;
				}
			}
			return -1;
		}
	</script>

</html>